package com.efast.cafe.portal.service.doc.impl;

import java.util.ArrayList;
import java.util.List;

import javax.swing.Spring;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.doc.DocGroupUserAuthFormBean;
import com.efast.cafe.portal.bean.doc.DocGroupUserAuthQueryBean;
import com.efast.cafe.portal.bean.doc.DocGroupUserAuthResBean;
import com.efast.cafe.portal.bean.doc.DocUserAuthResBean;
import com.efast.cafe.portal.dao.doc.DocAuthDao;
import com.efast.cafe.portal.dao.doc.DocUserDao;
import com.efast.cafe.portal.entity.doc.DocPurview;
import com.efast.cafe.portal.service.doc.IDocAuthService;
import com.efast.cafe.portal.service.doc.IDocUserService;

@Service
@Transactional
public class DocAuthServiceImpl implements IDocAuthService{
	
	@Autowired
	private DocAuthDao docAuthDao;
	@Autowired
	private IDocUserService docUserService;
	@Autowired
	private DocUserDao docUserDao;

	@Override
	public List<DocGroupUserAuthResBean> queryGroupUserAuthList(DocGroupUserAuthQueryBean queryBean) {
		queryBean.setCompanyCode(SpringWebUtils.getRequestCompany());
		return docAuthDao.queryGroupUserAuthList(queryBean);
		
	}

	@Override
	public List<DocUserAuthResBean> queryUserAuthDetail(Integer groupId) {
		String companyCode=SpringWebUtils.getRequestCompany();
        String username=SpringWebUtils.getRequestUser();
        return docAuthDao.queryUserAuthDetail(companyCode, username, groupId);
		
		 
	}

	@Override
	public void deleteUserAuth(List<String> usernames, Integer groupId) {
		String companyCode=SpringWebUtils.getRequestCompany();
		docAuthDao.deleteUserAuth(companyCode, usernames, groupId);
		
	}

	@Override
	public void saveUserAuth(DocGroupUserAuthFormBean bean) {
		String companyCode=SpringWebUtils.getRequestCompany();
		String username=SpringWebUtils.getRequestUser();
		//删除已有权限
		
		docAuthDao.deleteUserAuth(companyCode, bean.getUsernames(), bean.getGroupId());

		bean.setCompanyCode(companyCode);
		bean.setCurrentUser(username);
		docAuthDao.saveUserAuth(bean);
		
	}

	@Override
	public List<DocPurview> queryDocPurview(String type) {
		
		return docAuthDao.queryDocPurview(type);
	}

	@Override
	public void updateByUserAuth(DocGroupUserAuthFormBean bean) {
		List<String> list=new ArrayList<>();
		list.add(bean.getUsername());
		bean.setUsernames(list);
		saveUserAuth(bean);
		
		
		
	}

	@Override
	public List<DocUserAuthResBean> detailByUserAuth(DocGroupUserAuthFormBean bean) {
		bean.setCompanyCode(SpringWebUtils.getRequestCompany());
		//如果是管理员 则默认全部权限
		if(docUserDao.queryDocUser(bean.getUsername(),SpringWebUtils.getRequestCompany()).getDefaultUser()==1){
			bean.setGroupId(null);
		}
		return docAuthDao.detailByUserAuth(bean);
	}

}
